package com.johson.covid19server.controller;

import lombok.extern.slf4j.Slf4j;
import com.johson.covid19server.pojo.CovidCase;
import com.johson.covid19server.service.CovidCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestParam;

@Slf4j
@RestController
@RequestMapping("/gps")
public class GpsController {

    @Autowired
    private CovidCaseService covidCaseService;

    @GetMapping("/list")
    // @ResponseBody
    public String list(@RequestParam(value="queryDate", required=false) String queryDate) {
        log.info(String.format("开始查询GPS列表[%s]", queryDate));
        if (queryDate == null) {
            return "[]";
        }
        JSONArray gpsList = new JSONArray();
        try {
            List<CovidCase> covidCaseList = covidCaseService.queryByDate(queryDate);
            for (CovidCase covidCase : covidCaseList) {
                // log.info("covidCase: " + covidCase);
                JSONObject gps = new JSONObject();
                gps.put("longitude",covidCase.longitude);
                gps.put("latitude",covidCase.latitude);
                gps.put("casecode",covidCase.casecode);
                gps.put("sex",covidCase.sex);
                gps.put("age",covidCase.age);
                gps.put("adress",covidCase.adress);
                gpsList.add(gps);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询列表异常：{}", e.getMessage());
        }
        return gpsList.toJSONString();
    }

}